package com.cxs.subscrib.slice;

import com.cxs.subscrib.ResourceTable;
import com.cxs.subscrib.event.MyCommonEventSubscriber;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.MatchingSkills;
import ohos.rpc.RemoteException;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        findComponentById(ResourceTable.Id_btn_subscribe).setClickedListener(c -> subscribeEvent());
    }

    private void subscribeEvent() {
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent("com.cxs.commoneventtest.event");
        CommonEventSubscribeInfo subscribe = new CommonEventSubscribeInfo(matchingSkills);
        try {
            CommonEventManager.subscribeCommonEvent(new MyCommonEventSubscriber(subscribe));
            System.out.println("订阅事件结束了");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
